package com.shenfc.fxzj.action.admin;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.shenfc.fxzj.constants.CodeConstants;
import com.shenfc.fxzj.constants.SuperAdminEnum;
import com.shenfc.fxzj.domain.DataGridJson;
import com.shenfc.fxzj.domain.PageBean;
import com.shenfc.fxzj.domain.SysRole;
import com.shenfc.fxzj.domain.SysUser;
import com.shenfc.fxzj.service.ISysRoleRelService;
import com.shenfc.fxzj.service.ISysRoleService;
import com.shenfc.fxzj.service.ISysUserService;
import com.shenfc.fxzj.solr.BlogSolrIndex;
import com.shenfc.fxzj.util.HtmlUtil;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 管理员博客Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/admin/sysUser")
public class SysUserAdminAction extends BaseAction {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysRoleRelService sysRoleRelService;

    // 博客索引
    private BlogSolrIndex blogIndex = new BlogSolrIndex();

    private static String content;

    private final static Logger log = LoggerFactory.getLogger(SysUserAdminAction.class);

    @RequestMapping("/index")
    public ModelAndView index() throws Exception {
        return new ModelAndView("admin/sysUser/sysUserList");
    }

    @RequestMapping("/add")
    public ModelAndView add(SysUser sysUser, String page, String type) throws Exception {
        ModelAndView mav = new ModelAndView();
        List<SysRole> sysRoleList = sysRoleService.getAllSysRoles();
        mav.addObject("sysRoleList", sysRoleList);
        mav.addObject("type", type);
        if (sysUser.getId() == null) {

        } else {
            SysUser data = sysUserService.getSysUserById(sysUser.getId());
            mav.addObject("data", data);
        }
        mav.setViewName("/admin/sysUser/sysUserAdd");
        return mav;
    }

    /**
     * 添加或者修改博客信息
     */
    @RequestMapping("/save")
    @ResponseBody
    public JSONObject save(SysUser sysUser, HttpServletResponse response) throws Exception {
        JSONObject result = new JSONObject();
        try {
            if (sysUser.getId() == null) {
                sysUser.setSuperAdmin(SuperAdminEnum.NO.getKey());
                sysUser.setStatus(CodeConstants.YES);
                SimpleHash md5 = new SimpleHash("MD5", "654321", sysUser.getUserName(), 1);
                sysUser.setPassword(md5.toString());
                sysUser.setCreateTime(new Date());
                sysUser.setCreateUserId(getShiroSessionUserId());
                sysUserService.addSysUser(sysUser);
            } else {
                sysUser.setUpdateTime(new Date());
                sysUser.setUpdateUserId(getShiroSessionUserId());
                sysUserService.updateSysUser(sysUser);
            }
            result.put("success", true);
            result.put("msg", "保存成功");
        } catch (Exception e) {
            log.error("博客保存失败", e);
            result.put("success", false);
            result.put("msg", "保存失败");
        }
        return result;
    }

    /**
     * 分页查询博客信息
     */
    @RequestMapping("/list")
    public void list(PageBean pageBean, SysUser sysUser, HttpServletResponse response) {
        DataGridJson<SysUser> dataGridJson = new DataGridJson<SysUser>();
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("userName", sysUser.getUserName());
            map.put("status", sysUser.getStatus());
            PageInfo<SysUser> pageInfo = sysUserService.querySysUserPage(map, pageBean);
            List<SysUser> sysUserList = pageInfo.getList();
            dataGridJson.setData(sysUserList);
            dataGridJson.setCount(pageBean.getRecTotal());
            HtmlUtil.writerJson(response, dataGridJson);
        } catch (Exception e) {
            dataGridJson.setCode(1);
            dataGridJson.setMsg("请求出现异常，请联系管理员！");
            HtmlUtil.writerJson(response, dataGridJson);
        }
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public JSONObject delete(Long id, String status, HttpServletResponse response) throws Exception {
        JSONObject result = new JSONObject();
        try {
            sysUserService.updateStatus(id, status);
            result.put("success", true);
            result.put("msg", "修改成功");
        } catch (Exception e) {
            log.error("修改活动状态失败", e);
            result.put("success", false);
            result.put("msg", "修改失败");
        }
        return result;
    }

}
